package farmers.game;

import java.io.*;

public class Hero2Test {

	public static void main(String[] args) throws Exception {
		// 文件夹路径
		String srcPath = "E:\\game\\英雄无敌2\\怪物文件\\00 原版 Creature.cfg";
		String destPath = "E:\\game\\英雄无敌2\\怪物文件\\01 生产x5 Creature.cfg";
		StringBuffer result = readFile02(srcPath);
		System.out.println(result);
		
		writeFile(destPath, result.toString());
	}

	public static void writeFile(String fileName, String fileContent) {
		try {
			File f = new File(fileName);
			if (!f.exists()) {
				f.createNewFile();
			}
			OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "gbk");
			BufferedWriter writer = new BufferedWriter(write);
			writer.write(fileContent);
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取一个文本 一行一行读取
	 *
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static StringBuffer readFile02(String path) throws IOException {
		// 使用一个字符串集合来存储文本中的路径 ，也可用String []数组
		StringBuffer result = new StringBuffer();
		FileInputStream fis = new FileInputStream(path);
		// 防止路径乱码 如果utf-8 乱码 改GBK eclipse里创建的txt 用UTF-8，在电脑上自己创建的txt 用GBK
		InputStreamReader isr = new InputStreamReader(fis, "GB2312");
		BufferedReader br = new BufferedReader(isr);
		String line = "";
		while ((line = br.readLine()) != null) {
			// 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
			if(false){

			}
			else if(line.startsWith("生命点数")) {
				String key = line.split("=")[0];
				String value = line.split("=")[1];
				Integer temp = new Integer(value) + 0;
				value = temp.toString();

				result.append(key + "=" + value + "\r\n");
			}
			else if(line.startsWith("攻击力")) {
				String key = line.split("=")[0];
				String value = line.split("=")[1];
				Integer temp = new Integer(value) + 0;
				value = temp.toString();

				result.append(key + "=" + value + "\r\n");
			}
			else if(line.startsWith("防御力")) {
				String key = line.split("=")[0];
				String value = line.split("=")[1];
				Integer temp = new Integer(value) + 0;
				value = temp.toString();

				result.append(key + "=" + value + "\r\n");
			}
			else if(line.startsWith("最小伤害")) {
				String key = line.split("=")[0];
				String value = line.split("=")[1];
				Integer temp = new Integer(value) + 0;
				value = temp.toString();

				result.append(key + "=" + value + "\r\n");
			}
			else if(line.startsWith("最大伤害")) {
				String key = line.split("=")[0];
				String value = line.split("=")[1];
				Integer temp = new Integer(value) + 0;
				value = temp.toString();

				result.append(key + "=" + value + "\r\n");
			}
			else if(line.startsWith("基本产量")) {
				String key = line.split("=")[0];
				String value = line.split("=")[1];
				Integer temp = new Integer(value) * 5;
				value = temp.toString();

				result.append(key + "=" + value + "\r\n");
			}
			else if(line.startsWith("单位价格")) {
				String key = line.split("=")[0];
				String value = line.split("=")[1];
				Integer temp = new Integer(value) / 1;
				value = temp.toString();
				
				result.append(key + "=" + value + "\r\n");
			}
			else {
				result.append(line + "\r\n");
			}
			
			
		}
		br.close();
		isr.close();
		fis.close();
		return result;
	}
}
